package games.flvPlayer.data
{
	import flash.events.AsyncErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.media.SoundTransform;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	public class StreamManager extends EventDispatcher
	{
		public static const GET_METADATA:String = "get meta data";
		private var m_connection:NetConnection;
		private var m_stream:NetStream;
		private var m_video:Video;
		private var m_time:Number;
		private var m_url:String;
		private var m_totalTime:Number;
		private var m_soundControl:SoundTransform;
		private var m_keyFrames:Array;
		private var m_filepositions:Array;
		
		public function StreamManager(n:Video)
		{
			m_video = n;
			init();
		}
		
		//----------------------------提供给外部---------------------------\\
		public function get totalTime():Number
		{
			return m_totalTime;
		}
		
		public function get stream():NetStream
		{
			return m_stream;
		}
		
		public function play(n:String):void
		{
			m_url = n;
			m_video.visible = true;
			m_stream.play(n);
		}
		
		public function playPer(per:Number):void
		{
			var position:Number = getKeyFrameByPer(per);
			trace(position)
			var url:String = m_url + "?start=" + m_filepositions[position];
			m_stream.play(url);
			trace(url);
		}
		
		public function pause():void
		{
			m_stream.togglePause();
			
		}
		
		public function resume():void
		{
			m_stream.resume();
		}
		
		public function seek(n:Number):void
		{
			m_stream.seek(n);
		}
		
		public function seekPercent(n:Number):void
		{
			if(n > 100 || n < 0)return;
			seek( n * m_totalTime * .01);
		}
		
		public function stop():void
		{
			m_stream.close();
			m_video.visible = false;
		}
		
		public function resetVolume(n:Number):void
		{
			m_soundControl = new SoundTransform();
			m_soundControl.volume = n;
			m_stream.soundTransform = m_soundControl;
		}
		
		//----------------------------/提供给外部---------------------------\\
		
		private function init():void
		{
			m_connection = new NetConnection();
			m_connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			m_connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			m_connection.connect(null);
		}
		
		public function onMetaData(info:Object):void { 
			trace("onMetaData fired"); 
			m_totalTime = info.duration;  
			for(var i:String in info)
			{
				trace(i ,"***",info[i]);
			}
			if(info["keyframes"])
			{
				m_keyFrames = info["keyframes"].times;
				m_filepositions = info["keyframes"].filepositions;
			}else if(info["seekpoints"])
			{
				m_keyFrames = new Array();
				m_filepositions = new Array();
				for(var j:int = 0; j < info["seekpoints"].length; j++)
				{
					m_keyFrames.push(info["seekpoints"][j].time);
					m_filepositions.push( info["seekpoints"][j].offset);
					for(i in info["seekpoints"][j])
					{
						trace(i,"***********888",info["seekpoints"][j][i]);
					}
				}
			}
			for(i in info["keyframes"])
			{
				trace( i," ****** key fames********",info["keyframes"][i]);
			}
			dispatchEvent(new Event(GET_METADATA));
		} 
		
		public function onPlayStatus(info:Object):void
		{
			trace("onPlayStatus...");
		}
		
		private function netStatusHandler(event:NetStatusEvent):void {
			switch (event.info.code) {
				case "NetConnection.Connect.Success":
					connectStream();
					break;
				case "NetStream.Play.StreamNotFound":
					trace("Unable to locate video: " );
					break;
			}
		}
		
		private function connectStream():void {
			m_stream = new NetStream(m_connection);
			m_stream.client = this;
			m_stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			m_stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			m_video.attachNetStream(m_stream);
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event);
		}
		
		private function asyncErrorHandler(event:AsyncErrorEvent):void {
			// ignore AsyncErrorEvent events.
		}
		
		private function getKeyFrameByPer(per:Number):int
		{
			var time:int = getTimeByPer(per);
			trace("time..................",time);
			var n:Number;
			for(var i:int = 1; i < m_keyFrames.length; i++)
			{
				if(m_keyFrames[i-1] <= time && m_keyFrames[i] >= time)
				{
					n = (m_keyFrames[i]- time > time - m_keyFrames[i-1])?i-1:i;
					break;
				}
			}
			return n;
		}
		
		private function getTimeByPer(per:Number):int
		{
			var n:Number;
			n = int(per * m_totalTime * .01);
			return n;
		}
	}
}